define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'auth/admingroup/index' + location.search,
                    add_url: 'auth/admingroup/add',
                    edit_url: 'auth/admingroup/edit',
                    del_url: 'auth/admingroup/del',
                    multi_url: 'auth/admingroup/multi',
                    import_url: 'auth/admingroup/import',
                    table: 'admin_group',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                visible: false,
                showToggle: false,
                showColumns: false,
                search:false,
                showExport: false,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        // {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {

            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));

                Fast.api.teacher_Select('.select', {
                    multiple: true,
                    type:true,
                    callback: function (data) {
                        setTeacher(data);
                    }
                });


                var setTeacher = function (data) {
                    $('#list_content').show();
                    var congfu = '';
                    for(var i=0;i<data.length;i++){
                        if(!$('#data_'+data[i].id).html()){
                            var html = '<tr id="data_'+data[i].id+'">\n' +
                                '        <td><input type="hidden" name="row[admin_id][]" value="'+data[i].id+'">'+data[i].id+'</td>\n' +
                                '        <td>'+data[i].nickname+'</td>\n' +
                                '        <td><a href="javascript:void()" onclick="$(\'#data_'+data[i].id+'\').remove()" data-operate="remove" class="btn btn-sm btn-warning">移除</a></td>\n' +
                                '    </tr>';
                            $('#list_div').prepend(html);
                        }else{
                            congfu += data[i].nickname+',';
                        }
                    }
                    if(congfu){
                        layer.msg(congfu+'已存在');
                    }
                };
            }
        }
    };
    return Controller;
});
